package com.hong.spring.sepl;

import com.hong.spring.common.entity.User;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @Auther: csh
 * @Date: 2020/7/13 15:47
 * @Description:
 */
public class CompilerSample {
    public static void main(String[] args) {
        User user = new User("hong",29);
        //1.创建解析配置
        SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,CompilerSample.class.getClassLoader());
        //2.创建解释器
        SpelExpressionParser parser = new SpelExpressionParser(configuration);
        //3.创建取值上下文
        EvaluationContext cotext = new StandardEvaluationContext(user);
        //4.表达式
        String expression = "getAge()>20";
        //5.解析表达式
        SpelExpression spelExpression = parser.parseRaw(expression);
        //6.通过表达式求值
        System.out.println(spelExpression.getValue(cotext));
        System.out.println(spelExpression.getValue(cotext));

    }
}
